<template>
  <el-dialog title="部门信息编辑" :visible.sync="dialog_visible">
    <div class="form-padding">
      <el-form
        ref="dataForm"
        :rules="rules"
        :model="temp"
        label-position="right"
        auto-complete="on"
        label-width="90px"
        style="width: 100%;"
      >
        <el-form-item label="序号" prop="id">
          <el-input disabled v-model="temp.id" />
        </el-form-item>
        <el-form-item label="部门名称" prop="department">
          <el-input placeholder="请输入部门名称" v-model="temp.department" />
        </el-form-item>
        <el-form-item label="小组名称" prop="group">
          <el-input placeholder="请输入小组名称" v-model="temp.group" />
        </el-form-item>
      </el-form>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button @click="cancel">取消</el-button>
      <el-button type="primary" @click="submit">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { update } from "@/api/org_department";
export default {
  name: "departmentEdit",
  props: {
    show: {
      type: Boolean,
      default: false,
    },
    data: {
      type: Object,
      default: function () {
        return {};
      },
    },
  },
  data() {
    const validaterole_name = (rule, value, callback) => {
      if (value.length > 30) {
        callback(new Error("名称不能太长！"));
      } else {
        callback();
      }
    };
    return {
      temp: {
        id: "",
        department: "",
        group: "",
      },
      rules: {
        department: [
          { required: true, message: "部门名称不能为空", trigger: "blur" },
          { required: true, trigger: "blur", validator: validaterole_name },
        ],
        group: [
          { required: true, message: "小组名称不能为空", trigger: "blur" },
          { required: true, trigger: "blur", validator: validaterole_name },
        ],
      },
    };
  },
  computed: {
    dialog_visible: {
      get() {
        return this.show;
      },
      set(val) {
        this.$emit("update:show", val);
      },
    },
  },
  watch: {
    data: {
      handler(newValue, oldValue) {
        this.temp = Object.assign({}, this.data);
        // 当 watchedObject 或其内部属性变化时，这个函数被调用
      },
      deep: true,
    },
  },
  created() {},
  methods: {
    cancel() {
      this.dialog_visible = false;
    },
    submit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          const param = {
            id: this.temp.id,
            department: this.temp.department,
            group: this.temp.group,
          };
          update(param).then((response) => {
            if (response.data > 0) {
              setTimeout(() => {
                this.dialog_visible = false;
                this.$message({
                  type: "success",
                  message: "部门信息修改成功!",
                });
                this.$emit("success", param);
              }, 200);
            } else {
              this.$message({
                type: "info",
                message: "部门信息修改失败",
              });
            }
          });
        }
      });
    },
  },
};
</script>